-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CIP-0138 Builtin Array #6727
base: master
Are you sure you want to change the base?
CIP-0138 Builtin Array #6727
Conversation
92b8011
to
6de1458
Compare
6de1458
to
acdd3ef
Compare
acdd3ef
to
cfd35ae
Compare
cfd35ae
to
5de42d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the lack of Plinth versions of the builtins intentional?
All looks great. Kinda unfortunate that more and more is needed to add a new builtin, maybe we could automate some more. Although seems not too bad still.
plutus-core/testlib/PlutusCore/Generators/QuickCheck/Builtin.hs
Outdated
Show resolved
Hide resolved
let arrayOfInts = mkConstant @(Vector Integer) @DefaultUni () (Vector.fromList [1..10]) | ||
let term = apply () (tyInst () (builtin () ListToArray) integer) listOfInts | ||
typecheckEvaluateCekNoEmit def defaultBuiltinCostModelForTesting term @?= | ||
Right (EvaluationSuccess arrayOfInts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some edge cases would be nice, like an empty array etc.
You should've probably used
evals
:: DefaultUni `HasTermLevel` a
=> a
-> DefaultFun
-> [Type TyName DefaultUni ()]
-> [Term TyName Name DefaultUni DefaultFun ()]
-> TestNested
from the below, we use it for unit tests. Not important though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some edge cases would be nice, like an empty array etc.
Those will definitely get tested in the conformance tests later. Maybe it's OK to have some overlap here though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it's better to have some overlap here, since it's cheap.
term = apply () (tyInst () (builtin () LengthArray) integer) arrayOfInts | ||
typecheckEvaluateCekNoEmit def defaultBuiltinCostModelForTesting term @?= | ||
Right (EvaluationSuccess expectedLength) | ||
, testCase "indexArray" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also have some property tests, but feel free to add them in a follow-up.
Yes. |
5de42d2
to
911f19d
Compare
911f19d
to
8535806
Compare
8535806
to
d3942d7
Compare
d3942d7
to
1c8d186
Compare
1c8d186
to
fe5c0a3
Compare
fe5c0a3
to
2ac9dba
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks OK modulo a few minor quibbles. I'd change the costs in Default.Builtins
to be unimplementedCostingFunction
just to make sure that we don't get any surprises before the costing's done properly though.
DefaultUniArray _argUni -> pure $ Vector.length vec | ||
_ -> throwing _StructuralUnliftingError "Expected an array but got something else" | ||
{-# INLINE lengthArrayDenotation #-} | ||
in makeBuiltinMeaning lengthArrayDenotation (runCostingFunOneArgument . paramLengthArray) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't mind it we said that new builtins must go last and move caseList and caseData there.
Yes, I think we should move them too.
{-# INLINE nullListDenotation #-} | ||
in makeBuiltinMeaning | ||
nullListDenotation | ||
(runCostingFunOneArgument . paramNullList) | ||
|
||
toBuiltinMeaning _semvar LengthArray = | ||
let lengthArrayDenotation :: SomeConstant uni (Vector a) -> BuiltinResult Int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call this lengthOfArray
for consistency with lengthOfByteString
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind, @effectfully are you ok with it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry, I think it was me who gave you the bad advice.
@@ -293,6 +305,7 @@ addConstantRose (CostRose cost1 forest1) (CostRose cost2 forest2) = | |||
{-# INLINE addConstantRose #-} | |||
|
|||
instance ExMemoryUsage a => ExMemoryUsage [a] where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing to do with this PR really, but I wonder if we can/should make the default memory usage for lists the one that returns the length of the list (ie, the one for the ListCostedByLength
wrapper). That's a more natural measure of size for many applications (like listToArray
) since in general you'll only be copying pointers rather than reallocating all of the contents of the list. I think this got added for serialiseData
(because there you really do have to care about the contents) and it just ended up as the default. We'd have to check its uses carefully if we do want to change it though. Also ExMemoryUsage
isn't really the best name in this context since it's really just a size measure.
plutus-core/plutus-core/src/PlutusCore/Evaluation/Machine/ExMemoryUsage.hs
Show resolved
Hide resolved
@@ -62,3 +62,8 @@ instance PrettyDefaultBy config [a] => DefaultPrettyBy config (Set a) where | |||
defaultPrettyBy config = prettyBy config . Set.toList | |||
deriving via PrettyCommon (Set a) | |||
instance PrettyDefaultBy config (Set a) => PrettyBy config (Set a) | |||
|
|||
instance PrettyDefaultBy config [a] => DefaultPrettyBy config (Vector a) where | |||
defaultPrettyBy config = prettyBy config . toList |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if having identical textual representations could ever lead to ambiguity. I suppose in UPLC it'll always have list
or array
nearby anyway.
let arrayOfInts = mkConstant @(Vector Integer) @DefaultUni () (Vector.fromList [1..10]) | ||
let term = apply () (tyInst () (builtin () ListToArray) integer) listOfInts | ||
typecheckEvaluateCekNoEmit def defaultBuiltinCostModelForTesting term @?= | ||
Right (EvaluationSuccess arrayOfInts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some edge cases would be nice, like an empty array etc.
Those will definitely get tested in the conformance tests later. Maybe it's OK to have some overlap here though.
I wonder if we have to worry about Later: I think that it's OK. Even in |
@@ -91,6 +92,10 @@ instance HasFromBuiltin a => HasFromBuiltin (BuiltinList a) where | |||
type FromBuiltin (BuiltinList a) = [FromBuiltin a] | |||
fromBuiltin (BuiltinList xs) = map fromBuiltin xs | |||
|
|||
instance HasToBuiltin a => HasToBuiltin (Vector a) where | |||
type ToBuiltin (Vector a) = BuiltinArray (ToBuiltin a) | |||
toBuiltin = useToOpaque (BuiltinArray . map toBuiltin) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is causing a CI failure because BultinArray
doesn't exist yet (in this PR). Can we just remove this from the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it fails atm. My idea was to first merge adjacent PR (related changes in Plinth) into this one first.
Closes #6717
Data.Vector.Strict
LengthArray
ListToArray
The PR is ready for review but build is not working until we update flake inputs (need vector 0.13.2)